package wensheng.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wensheng.stu.common.CustomExceptionHandler;
import wensheng.stu.entity.Category;
import wensheng.stu.entity.Dish;
import wensheng.stu.entity.Setmeal;
import wensheng.stu.mapper.CategoryMapper;
import wensheng.stu.service.CategoryService;
import wensheng.stu.service.DishService;
import wensheng.stu.service.SetMealService;

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealService setMealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        int count  = dishService.count(queryWrapper);
        if(count >0){
            throw new CustomExceptionHandler("当前分类下关联了菜品,不能删除");
        }


        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setMealService.count(setmealLambdaQueryWrapper);
        if(count2>0){
            throw new CustomExceptionHandler("关联了相关套餐,不能删除");
        }

        super.removeById(id);

    }
}
